const {createServer} =require('node:http');
const url = require('url')
const hostname = '127.0.0.1';
const port = 3000;


const mongoose = require('mongoose')
const Goods = require('./model/goods')
mongoose.connect('mongodb://node:123456@114.132.75.251:27017/nodeTest').then(r=>{
    console.log('数据库连接成功')
});

const server = createServer(async (req,res)=>{
    res.statusCode = 200;
    res.writeHead(200,{'Content-type':'application/json'});
    const urlObj = url.parse(req.url ,true);
    const query = urlObj.query;
    console.log(query)
    if(query&&query.title && query.price ){
        let data = await Goods.create(query)
        res.end(JSON.stringify(data));
    }else {
        res.end(JSON.stringify([{msg:'参数错误，不能存储'}]))
    }



});
server.listen(port,hostname,()=>{
    console.log('Server running at http://${hostname}:${port}/');
})